// 路由模块
import { createRouter, createWebHashHistory, routerKey } from "vue-router";
// 创建路由实例对象
const routes = createRouter({ // create routes
    history: createWebHashHistory(),

    routes: [
        { path: "/", redirect: "/movie" },
        {
            path: "/home",
            component: () =>
                import ("./components/Home.vue")
        }, // add Home route
        {
            path: "/about",
            component: () =>
                import ("./components/About.vue"),

            children: [{
                    path: "tab1",
                    component: () =>
                        import ("./components/pages/Tab1.vue")
                },
                {
                    path: "tab2",
                    component: () =>
                        import ("./components/pages/Tab2.vue")
                }
            ]

        }, // add About route
        {
            path: "/movie",
            component: () =>
                import ("./components/Movie.vue"),
            children: [{
                path: ":id", // 动态路由
                name: "MovieDetails",
                component: () =>
                    import ("./components/MovieDetails.vue"),
                props: true
            }]
        }, {
            path: "/login",
            name: "Login",
            component: () =>
                import ("./components/login.vue")
        },
    ]
})

routes.beforeEach((to, from, next) => {
    let isLogin = false;
    if (to.name === "MovieDetails") {

        if (isLogin) {
            next()
        } else {
            next({ name: "Login" });
        }
    } else {
        next();
    }
})
export default routes; // export routes 导出路由实例对象